package com.msmall.community.admin.action;

import java.util.List;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantUrl;
import com.msmall.community.common.Configuration;
import com.msmall.community.team.model.Team;
import com.msmall.community.team.model.TeamProperty;
import com.msmall.community.team.model.TeamUser;
import com.msmall.community.team.service.TeamManager;

// 刷新内存
public class TeamAction extends HTMLBaseAction{

	private static final long serialVersionUID = -9094290050408783459L;

	private Long teamid;
	private Long propertyid;
	private Long tuid;
	private String propertyKey;
	private String propertyValue;
	private Team team;
	private TeamProperty property;
	private TeamUser teamUser;
	private List<TeamProperty> properties;
	private List<TeamUser> teamUsers;
	private TeamManager teamManager;
	
	
	// 进入team管理首页，显示所有TEAM列表
	public String execute(){
		page.setPageSize(20);
		page.setClazz(Team.class);
		page.setOrderBy("createTime");
		page.setOrder("DESC");
		teamManager.getPagination(page);
		page.setUrl(ConstantUrl.ADMIN_TEAM_INDEX);
		return SUCCESS;
	}
	//进入Team管理页面
	public String update(){
		team = (Team) teamManager.get(Team.class, teamid);
		return SUCCESS;
	}
	//保持修改的TEAM信息
	public String save(){
		teamManager.saveObject(team);
		return SUCCESS;
	}
	//获得组的所有用户信息
	public String getUsers(){
		teamUsers = teamManager.getMembers(teamid, -1);
		return SUCCESS;
	}
	public String getUser(){
		teamUser = (TeamUser) teamManager.get(TeamUser.class, tuid);
		return SUCCESS;
	}
	public String saveUser(){
		TeamUser creator = teamManager.getTeamCreator(teamUser.getTeamid());
		if(creator.getUserid().longValue() != teamUser.getUserid().longValue()){//只有一个组长
			if(TeamUser.POSITION_CHAIRMAN.intValue() == teamUser.getPosition()){
				teamUser.setPosition(TeamUser.POSITION_NORMAL);
			}
		}
		//副组长数量限制
		if(TeamUser.POSITION_VICE_CHAIRMAN.intValue() == teamUser.getPosition()){
			List<TeamUser> normalManagers = teamManager.getTeamManagers(teamUser.getTeamid(), false);
			if(normalManagers != null && normalManagers.size() == Configuration.getIntegerValue(Configuration.TEAM_DEFAULT_VICE_CHAIRMAN_COUNT)){
				teamUser.setPosition(TeamUser.POSITION_NORMAL);
			}
		}
		teamManager.saveObject(teamUser);
		teamid = teamUser.getTeamid();
		return SUCCESS;
	}
	//查看一个组的可见性
	public String teamVisit(){
		property = teamManager.getTeamVisit(teamid);
		return SUCCESS;
	}
	//保存一个组的可见性
	public String saveTeamVist(){
		property = teamManager.getTeamVisit(teamid);
		property.setPropertyValue(propertyValue);
		teamManager.saveObject(property);
		return SUCCESS;
	}
	//查看一个组的共享组
	public String teamShare(){
		property = teamManager.getSharedTeam(teamid);
		return SUCCESS;
	}
	public String saveTeamShare(){
		property = teamManager.getSharedTeam(teamid);
		property.setPropertyValue(propertyValue);
		teamManager.saveObject(property);
		return SUCCESS;
	}
	// 查看组的授权用户
	public String teamAuthUser(){
		property = teamManager.getAuthrizedUser(teamid);
		return SUCCESS;
	}
	public String saveTeamAuthUser(){
		property = teamManager.getAuthrizedUser(teamid);
		property.setPropertyValue(propertyValue);
		teamManager.saveObject(property);
		return SUCCESS;
	}
	// 查看组的加入申请是否需要审核
	public String teamApplyAudit(){
		property = teamManager.getTeamApplyAudit(teamid);
		return SUCCESS;
	}
	public String saveTeamApplyAudit(){
		property = teamManager.getTeamApplyAudit(teamid);
		property.setPropertyValue(propertyValue);
		teamManager.saveObject(property);
		return SUCCESS;
	}
	
	public Long getTeamid() {
		return teamid;
	}

	public void setTeamid(Long teamid) {
		this.teamid = teamid;
	}

	public Team getTeam() {
		return team;
	}
	public List<TeamProperty> getProperties() {
		return properties;
	}
	public void setProperties(List<TeamProperty> properties) {
		this.properties = properties;
	}
	public Long getPropertyid() {
		return propertyid;
	}
	public void setPropertyid(Long propertyid) {
		this.propertyid = propertyid;
	}
	public String getPropertyKey() {
		return propertyKey;
	}
	public void setPropertyKey(String propertyKey) {
		this.propertyKey = propertyKey;
	}
	public String getPropertyValue() {
		return propertyValue;
	}
	public void setPropertyValue(String propertyValue) {
		this.propertyValue = propertyValue;
	}
	public TeamProperty getProperty() {
		return property;
	}
	public void setProperty(TeamProperty property) {
		this.property = property;
	}

	public void setTeam(Team team) {
		this.team = team;
	}
	public Long getTuid() {
		return tuid;
	}
	public void setTuid(Long tuid) {
		this.tuid = tuid;
	}
	public TeamUser getTeamUser() {
		return teamUser;
	}
	public void setTeamUser(TeamUser teamUser) {
		this.teamUser = teamUser;
	}
	public List<TeamUser> getTeamUsers() {
		return teamUsers;
	}
	public void setTeamUsers(List<TeamUser> teamUsers) {
		this.teamUsers = teamUsers;
	}
	public void setTeamManager(TeamManager teamManager) {
		this.teamManager = teamManager;
	}
}